package com.imoresoft.agile.action;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.sql.dao.impl.SuperDaoImpl;
import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.DatasetList;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;
import com.imoresoft.agile.builder.IModuleBuilder;
import com.imoresoft.agile.builder.ModuleBuilderFactory;
import com.imoresoft.agile.exception.ProException;
import com.imoresoft.agile.top.TopConstants;
import com.imoresoft.agile.top.TopService;
import com.imoresoft.magic.util.StringUtil;
import com.imoresoft.magic.util.Utils;
import com.taobao.api.domain.Item;

public class UninstallAction extends BaseAction {

	private static final long serialVersionUID = 6466103688738653446L;

	private TopService topService = null;
	private String result = "";
	private String installPlaces = null;
	private String cats= null;
	private String installItemIids = null;
	private String promot = null;
	private String title = null;

	public UninstallAction() {
		topService = new TopService(TopConstants.EVN_SWITCH,
				TopConstants.APP_KEY, TopConstants.APP_SECRET);
	}

	public String getPromot() {
		return promot;
	}

	public void setPromot(String promot) {
		this.promot = promot;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getInstallPlaces() {
		return installPlaces;
	}

	public void setInstallPlaces(String installPlaces) {
		this.installPlaces = installPlaces;
	}

	public String getCats() {
		return cats;
	}

	public void setCats(String cats) {
		this.cats = cats;
	}

	public String getInstallItemIids() {
		return installItemIids;
	}

	public void setInstallItemIids(String installItemIids) {
		this.installItemIids = installItemIids;
	}
	
	@SuppressWarnings("unchecked")
	public String uninstall() {
		try {
			List<String> iidList = getInstallIids(promot, cats, installItemIids);
			
			long numIid = -1;
			String [] uninstallTitles = StringUtil.buildModuleTitles(installPlaces);
			String nick = pd.getTopNick();
			String sessionId = pd.getTopSessionId();
			for(String iid : iidList) {
				numIid = Long.parseLong(iid);
				topService.unintallPromotionModule(sessionId, nick, numIid, uninstallTitles);
			}
		} catch (ProException e) {
			logger.error("", e);
			result = "卸载时出错，请稍后重试或联系我们";
			return SUCCESS;
		} catch (Exception e) {
			logger.warn("卸载出现错误", e);
			result = "卸载时出错，请稍后重试或联系我们";
		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	private List<String> getInstallIids(String ... settings) throws ProException {
		String installSetting = settings[0];
		String sessionId = pd.getTopSessionId();
		String nick = pd.getTopNick();

		List<String> iidList = new ArrayList<String>();
		if ("custom".equals(installSetting)) {
			String staticSetting = settings[2];
			for (String iid : staticSetting.split(",")) {
				iidList.add(iid);
			}
		} else if ("someCategory".equals(installSetting)) {
			String sellerCids = settings[1];
			List<Item> itemList = topService.getOnSaleItemsOfSpecificSellerCat(
					sessionId, nick, sellerCids);
			for (Item item : itemList) {
				iidList.add(item.getNumIid().toString());
			}
		} else if ("itemCategory".equals(installSetting)) {
			String itemCids = settings[1];
			List<Item> itemList = null;//topService.getOnSaleItemsOfSpecificItemCat(
					//sessionId, nick, itemCids);
			for (Item item : itemList) {
				iidList.add(item.getNumIid().toString());
			}
		} else if ("onSale".equals(installSetting)) {
			List<Item> itemList = topService.getOnSaleItems(sessionId, nick);
			for (Item item : itemList) {
				iidList.add(item.getNumIid().toString());
			}
		} else if ("all".equals(installSetting)) {
			List<Item> itemList = topService.getAllItems(sessionId, nick);
			for (Item item : itemList) {
				iidList.add(item.getNumIid().toString());
			}
		}
		return iidList;
	}
}
